package com.lap.auth.domain.menu.service;

import com.lap.auth.domain.menu.Menu;
import com.lap.auth.domain.menu.repository.MenuRepository;
import com.lap.auth.domain.shared.errors.UserError;
import com.lap.framework.exception.BizException;
import lombok.RequiredArgsConstructor;

/**
 * 校验规则
 *
 * <ol>
 *   <li>链路 :领域服务(MenuService)->领域校验(MenuValidator)->领域对象。上述各层都可能不存在
 *   <li>被多个地方调用，或者自己领域不写服务类，就单独定义校验
 *   <li>单独自己服务调用，写在menuService即可
 *   <li>中断只能在这层，不要在往领域对象放，防止过深层级
 * </ol>
 */
@RequiredArgsConstructor
public class MenuValidator {

  private final MenuRepository menuRepository;

  public Menu assertMenuExists(Integer id) {
    return menuRepository
        .findById(id)
        .orElseThrow(() -> BizException.newBiz(UserError.MENU_NOT_FOUND));
  }
}
